var layer = null;
var form = null;
var menuId = getUrlParam('menuId');
layui.use(['form', 'layer'], function () {
    form = layui.form;
    layer = layui.layer;
    //监听提交
    var menu;
    axios.all([axios.get(ctx + 'sys/menu/info/' + menuId), axios.get(ctx + 'sys/menu/select')]).then(axios.spread(function (info, select) {
        menu = info.data.menu;
        var menuList =select.data.menuList;
        var optionsHtml = '';
        var formHtml = '';
        menuList.forEach(function (item) {
            if(item.menuId === menu.parentId) {
                console.log('checked');
                optionsHtml += '<option selected value="'+ item.menuId + '">' + item.name + '</option>';
            }else {
                optionsHtml += '<option value="'+ item.menuId + '">' + item.name + '</option>';
            }
        });
        if (menu.type === 0) {
            formHtml +=
                  '<div class="layui-form-item">'
                +   '<label class="layui-form-label">类型</label>'
                +       '<div class="layui-input-block">'
                +           '<input type="text" name="name" disabled autocomplete="off" placeholder="" value="' + (menu.type === 0 ? '目录' :  menu.type === 1 ? '路径' : '按钮') + '" class="layui-input">'
                +       '</div>'
                + '</div>'
                + '<div class="layui-form-item">'
                + '<label class="layui-form-label">*目录名称</label>'
                + '<div class="layui-input-block">'
                + '<input type="text" name="name" autocomplete="off" placeholder="目录名称" value="' + menu.name + '" class="layui-input">'
                + '</div>'
                + '</div>'
                + '<div class="layui-form-item">'
                + '<label class="layui-form-label">*上级目录</label>'
                + '<div class="layui-input-block">'
                + '<select name="parentId" id="top-menu" lay-filter="top-menu">'
                + optionsHtml
                + '</select>'
                + '</div>'
                + '</div>'
        } else if (menu.type === 1) {
            formHtml +=
                '<div class="layui-form-item">'
                +   '<label class="layui-form-label">类型</label>'
                +       '<div class="layui-input-block">'
                +           '<input type="text" name="name" disabled autocomplete="off" placeholder="" value="' + (menu.type === 0 ? '目录' : menu.type === 1 ? '路径' : '按钮') + '" class="layui-input">'
                +       '</div>'
                + '</div>'
                + '<div class="layui-form-item">'
                + '<label class="layui-form-label">*菜单名称</label>'
                + '<div class="layui-input-block">'
                + '<input type="text" name="name" autocomplete="off" placeholder="目录名称" value="' + menu.name + '" class="layui-input">'
                + '</div>'
                + '</div>'
                + '<div class="layui-form-item">'
                + '<label class="layui-form-label">*上级目录</label>'
                + '<div class="layui-input-block">'
                + '<select name="parentId" id="top-menu" lay-filter="top-menu">'
                + optionsHtml
                + '</select>'
                + '</div>'
                + '</div>'
                + '<div class="layui-form-item">'
                + '<label class="layui-form-label">*路径</label>'
                + '<div class="layui-input-block">'
                + '<input type="text" name="url" autocomplete="off" placeholder="路径" value="' + menu.url + '" class="layui-input">'
                + '</div>'
                + '</div>'
        } else if (menu.type === 2) {
            formHtml +=
             '<div class="layui-form-item">'
             +   '<label class="layui-form-label">类型</label>'
             +       '<div class="layui-input-block">'
             +           '<input type="text" name="name" disabled autocomplete="off" placeholder="" value="' + (menu.type === 0 ? '目录': menu.type === 1 ? '路径' : '按钮') + '" class="layui-input">'
             +       '</div>'
             + '</div>'
            + '<div class="layui-form-item">'
            + '<label class="layui-form-label">*按钮名称</label>'
            + '<div class="layui-input-block">'
            + '<input type="text" name="name" autocomplete="off" placeholder="目录名称" value="' + menu.name + '" class="layui-input">'
            + '</div>'
            + '</div>'
            + '<div class="layui-form-item">'
            + '<label class="layui-form-label">*上级目录</label>'
            + '<div class="layui-input-block">'
            + '<select name="parentId" id="top-menu" lay-filter="top-menu">'
            + optionsHtml
            + '</select>'
            + '</div>'
            + '</div>'
            + '<div class="layui-form-item">'
            + '<label class="layui-form-label">*授权标识</label>'
            + '<div class="layui-input-block">'
            + '<input type="text" name="perms" autocomplete="off" placeholder="授权标识" value="' + menu.perms + '" class="layui-input">'
            + '</div>'
            + '</div>'
        }
        $('.wrapper').html(formHtml);
        form.render();
    }));
    form.on('submit(edit)', function (data) {
        data.field['menuId'] = menuId;
        data.field['type'] = menu.type;
        edit(data.field);
        return false;
    });
});
$('#cancel').click(function () {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
});

function edit(data) {
    axios.post(ctx + 'sys/menu/update', data)
        .then(function (res) {
            if (res.data.code == 500) {
                layer.msg(res.data.msg);
            } else {
                layer.msg('添加成功');
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            }
        }).catch(function (err) {
        console.log(err)
    });
}